﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class ProbationController : Controller
    {
        private IEmployeeRepository employeeRepository;
        private IUnitOfWork unitOfWork;
        public ProbationController(IEmployeeRepository employeeRepository,
                                   IUnitOfWork unitOfWork)
        {
            this.employeeRepository = employeeRepository;
            this.unitOfWork = unitOfWork;
        }

        //将使用期Employee转换为ProbationModel,显示到Telrki的Grid当中,用于查看那些员工需要转正
        private IList<ProbationModel> EmployeesToProbationModel()
        {
            IList<ProbationModel> probationEmployees = new List<ProbationModel>();
            var employees = employeeRepository.GetMany(e => e.EmployeeStatus == "试用状态").ToList();
            foreach (Employee employee in employees)
            {
                probationEmployees.Add(new ProbationModel()
                                           {
                                               ID = employee.ID,
                                               Name = employee.Name,
                                               Sex = employee.Sex,
                                               Department = employee.Department.Name,
                                               Post = employee.Post.Title,
                                               JoinTime = employee.JoinTime,
                                               ProbationDates = employee.ProbationDates
                                           });
            }
            return probationEmployees;
        }
        //将Employee的概要数据通过BiographyOutline显示到Grid当中去
        public PartialViewResult Index()
        {
            return PartialView(EmployeesToProbationModel());
        }
        //用于分页排序和筛选
        [GridAction]
        public ActionResult TelerikAjax()
        {
            return View(new GridModel(EmployeesToProbationModel()));
        }
        //根据员工编号来将员工转正
        [HttpGet]
        public PartialViewResult PassProbation(int id)
        {
            Employee employee = employeeRepository.GetById(id);
            ProbationModel probationModel;
            if (ModelState.IsValid)
            {
                probationModel = new ProbationModel()
                                                    {
                                                        ID = employee.ID,
                                                        Name = employee.Name,
                                                        JoinTime = employee.JoinTime,
                                                        ProbationDates = employee.ProbationDates
                                                    };
            }
            else
            {
                probationModel = new ProbationModel();
            }

            return PartialView(probationModel);
        }
        [HttpPost]
        public ActionResult PassProbation(ProbationModel probationModel)
        {
            Employee employee = employeeRepository.GetById(probationModel.ID);
            string result = probationModel.ProbationResult;
            switch (result)
            {
                case "拟予辞退":
                    Session["url"] = "/Biography/Dimission/" + probationModel.ID;//跳转的离职页面
                    return RedirectToAction("Index", "HumanResources");
                case "延长试用":
                    employee.ProbationDates += probationModel.ExtendDates;//增加试用期
                    break;
                default:
                    employee.EmployeeStatus = "正式状态";
                    break;
            }
            employeeRepository.Update(employee);
            unitOfWork.Commit();
            Session["url"] = "/Probation/Index/";
            return RedirectToAction("Index", "HumanResources");
        }
    }
}
